#!/usr/bin/env bash

# Copyright 2017 The Bazel Authors. All rights reserved.

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

#    http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# --- begin runfiles.bash initialization v3 ---
# Copy-pasted from the Bazel Bash runfiles library v3.
set -uo pipefail; set +e; f=bazel_tools/tools/bash/runfiles/runfiles.bash
source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
  source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
  source "$0.runfiles/$f" 2>/dev/null || \
  source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
  source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
  { echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
# --- end runfiles.bash initialization v3 ---

@@GENERATED_MESSAGE@@

set -euo pipefail

GAZELLE_PATH=@@GAZELLE_PATH@@
ARGS=@@ARGS@@
GOTOOL=@@GOTOOL@@
REPO_CONFIG_PATH=@@REPO_CONFIG_PATH@@
WORKSPACE="@@WORKSPACE@@"

@@ENV@@

# set_goroot attempts to set GOROOT to the SDK used by rules_go. gazelle
# invokes tools inside the Go SDK for dependency management. It's good to
# use the SDK used by the workspace in case the Go SDK is not installed
# on the host system or is a different version.
function set_goroot {
  local gotool
  gotool=$(rlocation "$GOTOOL")
  if [ -z "$gotool" ]; then
    echo "$0: warning: could not locate GOROOT used by rules_go" >&2
    return
  fi
  GOROOT=$(cd "$(dirname "$gotool")/.."; pwd)
  export GOROOT
  if type cygpath >/dev/null 2>&1; then
    # On Windows, convert the path to something usable outside of bash.
    GOROOT=$(cygpath -w "$GOROOT")
  fi
}

# If arguments were provided on the command line, either replace or augment
# the generated args.
case "${1-}" in
  "fix" | "update" | "help" | "update-repos")
    ARGS=("$@")
    ;;
  *)
    ARGS+=("$@")
    ;;
esac

# Invoke Gazelle.
# Note that we don't change directories first; if we did, Gazelle wouldn't be
# able to find runfiles, and some extensions rely on that. Gazelle can use
# BUILD_WORKSPACE_DIRECTORY to interpret relative paths on the command line.
set_goroot
gazelle_path=$(rlocation "$GAZELLE_PATH")
if [ -z "$gazelle_path" ]; then
  echo "error: could not locate gazelle binary" >&2
  exit 1
fi
if [[ -n "${TEST_WORKSPACE+x}" && -n "$WORKSPACE" ]]; then
  BUILD_WORKSPACE_DIRECTORY="$(dirname "$(readlink ${WORKSPACE})")"
  export BUILD_WORKSPACE_DIRECTORY
fi
if [ -z "${BUILD_WORKSPACE_DIRECTORY-}" ]; then
  echo "error: BUILD_WORKSPACE_DIRECTORY not set" >&2
  exit 1
fi

# Determine if we are running the fix/update command
if [[ ${#ARGS[@]} -gt 0 ]]; then
  case "${ARGS[0]}" in
    "fix" | "update")
      is_fix_or_update="true"
      ;;
    *)
      is_fix_or_update="false"
      ;;
  esac
fi

# When running with Bzlmod, there is no WORKSPACE file for Gazelle to read
# the definitions of go_repository rules from. Instead, we pass the path to
# the repo config file as a flag.
if [[ "${is_fix_or_update:-}" == "true"  ]] && [[ -n $REPO_CONFIG_PATH ]]; then
  ARGS=("${ARGS[0]}" "-bzlmod" "-repo_config" "$(rlocation "$REPO_CONFIG_PATH")" "${ARGS[@]:1}")
fi

runfiles_export_envvars
"$gazelle_path" "${ARGS[@]}"
